package com.fanxuankai.boot.monitor;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * 企业微信
 *
 * @author fanxuankai
 */
public class WechatNotifier implements Notifier<Message> {
    private final String webhook;
    private final RestTemplate restTemplate;

    public WechatNotifier(String webhook, RestTemplate restTemplate) {
        this.webhook = webhook;
        this.restTemplate = restTemplate;
    }

    @Override
    public void notice(Message message) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgtype", "markdown");
        JSONObject markdown = new JSONObject();
        markdown.put("content", markdown(message));
        jsonObject.put("markdown", markdown);
        HttpEntity<Object> entity = new HttpEntity<>(jsonObject, headers);
        restTemplate.postForObject(webhook, entity, Void.class);
    }
}
